﻿/// <reference path="jquery-1.8.0.min.js"/>
/// <reference path="FusionCharts/FusionCharts.js"/>
/// <reference path="jquery.markitup.js"/>

var pageCount = 0;

$(function () {
    LoadGridView();
    $("#divShowAddTask").css("display", "none");
    $("#divDefault").click();
    $(".date-pick").datepicker({ minDate: 0 });
    $(".date-pick-nodate").datepicker({});
});


function LoadGridView() {
    $.ajax({
        type: "GET",
        url: "TaskGridList.aspx",
        data: getTaskConditions(1),
        cache: false,
        success: function (result) {
            var obj = $(result).find("#divGridView");
            pageCount = obj.find("#hiddenPageCount").val();
            $("#content").html(obj[0].outerHTML);

            InintPageSpan();

            $("#divPage").css("display", "inline");
        }
    });
}

//Search Button
$("#btnSearch").live("click", function () {

    $("#divShowTask").css("display", "");
    $("#divShowAddTask").css("display", "none");

    var index = $("#hiddenCurrentIndex").val();
    SearchGridViewByCondition(1, "all");
});

//初始化翻页数
function InintPageSpan() {

    $("#spCount").text($("#hiddenCount").val());
    $("#spPageCount").text(pageCount);
    $("#spCurrentIndex").text($("#hiddenCurrentIndex").val());
}

//根据条件查询GridView
function SearchGridViewByCondition(index, type) {
    $.ajax({
        type: "GET",
        url: "TaskGridList.aspx",
        cache: false,
        data: getTaskConditions(index, type),
        success: function (result) {
            var obj = $(result).find("#divGridView");
            $("#content").html(obj[0].outerHTML);

            InintPageSpan();
            $("#divPage").css("display", "inline");
        }
    });
}

//初始化查询条件
function getTaskConditions(index, type) {
    var searchKey = $("#ddlCols").val();
    var searchValue = $("#txtCondition").val();

    if (type == "all") {
        index = 1;
    }
    else {
        index = $("#hiddenCurrentIndex").val() == "" ? $("#hiddenCurrentIndex").val(1).val() : $("#hiddenCurrentIndex").val();
    }

    return { "searchData": "{'" + searchKey + "': '" + searchValue + "', 'Index': " + index + "}" }
}


//分页操作
$("#lbFirst,#lbPrev,#lbNext,#lbLast").live("click", function () {
    var actionID = $(this).attr("id");
    var currentIndex = parseInt($("#hiddenCurrentIndex").val());
    var index;

    switch (actionID) {
        case "lbFirst":
            index = 1;
            break;
        case "lbPrev":
            index = currentIndex - 1 > 0 ? currentIndex - 1 : 1;
            break;
        case "lbNext":
            index = currentIndex + 1 > pageCount ? currentIndex : currentIndex + 1;
            break;
        case "lbLast":
            index = pageCount;
            break;
    }

    $("#hiddenCurrentIndex").val(index);
    SearchGridViewByCondition(index, "");
});


//根据不同div状态查询GridView
function getGridViewByTaskDiv(searchKey, searchValue) {
    $.ajax({
        type: "GET",
        url: "TaskGridList.aspx",
        cache: false,
        data: getTaskDiv(searchKey, searchValue),
        success: function (result) {
            var obj = $(result).find("#divGridView");
            $("#content").html(obj[0].outerHTML);

            InintPageSpan();
            $("#divPage").css("display", "inline");
        }
    });
}

//获取TaskDiv查询条件
function getTaskDiv(searchKey, searchValue) {
    var index;

    if (searchKey == "all") {
        index = 1;
    }
    else {
        index = $("#hiddenCurrentIndex").val() == "" ? $("#hiddenCurrentIndex").val(1).val() : $("#hiddenCurrentIndex").val();
    }

    return { "searchData": "{'" + searchKey + "': '" + searchValue + "', 'Index': " + index + "}" }
}


$("#divMyTask,#divNewTask,#divProcessTask,#divAllTask").live("click", function () {
    var $obj = $(this);
    var searchKey = $obj.attr("searchkey");
    var searchValue = $obj.attr("searchvalue");

    if (searchKey == "eid") {
        searchValue = $("#hiddenEid").val();
    }
    getGridViewByTaskDiv(searchKey, searchValue);
});

//添加任务
$("#btnAddTask").live("click", function () {

    $("#divShowTask,#divShowHistory").css("display", "none");
    $("#divShowAddTask").css("display", "");

    $.ajax({
        type: "GET",
        url: "AddTask.aspx",
        cache: false,
        success: function (result) {
            var obj = $(result).find("#divAddTask");
            $("#divShowAddTask").html(obj[0].outerHTML);

            $("#txtHopeDate").datepicker({ minDate: 0 });
            InitEdit("txtAddContent");
        }
    });
});

//点击GridView行
$("tr[grid]").live("click", function () {
    var obj = $(this);
    var taskID = obj.attr("grid");

    $.ajax({
        type: "GET",
        url: "History.aspx",
        cache: false,
        data: { "taskID": taskID },
        success: function (result) {
            var obj = $(result).find("#divHistory");
            $("#content").html(obj[0].outerHTML);

            InitEdit("txtHistoryContent");
            $("#divPage").css("display", "none");
        }
    });
});


//初始化文本编辑器
function InitEdit(id) {
    $('#' + id).markItUp(mySettings);
    $('#emoticons a').click(function () {
        emoticon = $(this).attr("title");
        $.markItUp({ replaceWith: emoticon });
    });
}

//查询回复人详细信息
$("a[eid]").live("click", function () {
    var $obj = $(this);
    var eid = $obj.attr("eid");

    $.ajax({
        type: "GET",
        url: "ViewDetail.aspx",
        cache: false,
        data: { "eid": eid },
        success: function (result) {
            var obj = $(result).find("#divDetail");
            $("#divShowDetail").html(obj[0].outerHTML);
        }
    });
});

//获取添加任务数据
function getAddTaskData() {
    var projectId = $("#ddlProject").val();
    var importance = $("#spImportant").val();
    var hopeTime = $("#txtHopeDate").val();
    var subject = $("#txtTitle").val();
    var content = $("#txtContent").val();
    var actionType = "create";

    return { "type": "addTask", "addData": "{'actionType': '" + actionType + "','projectId': '" + projectId + "', 'importance': '" + importance + "','hopeTime': '" + hopeTime + "','subject': '" + subject + "','content': '" + content + "'}" }
}

//添加Task信息
$("#btnSubmit").live("click", function () {
    $.ajax({
        type: "GET",
        url: "AjaxHandler.ashx",
        cache: false,
        data: getAddTaskData(),
        success: function (result) {
            if (result == "true") {
                alert('添加成功');
            }
            else {
                alert('添加失败');
            }
        }
    });
});

//回复Task信息
$("#btnReply").live("click", function () {

    var actionType = "replay";
    var taskID = $("#hiddenTaskID").val();
    var content = $("#txtContent").val();
    var dataChange_LastTime = $("#lblEndDate").val();
    var projectId = $("#ddlProject").val();
    var subject = $("#lblSubject").text();

    var state = $("#ddlProcess").val();
    var handleMan = $("#ddlHandleMan").val();
    var importance = $("#ddlImportance").val();

    if ($("#cbIsAssign")[0].checked) {
        actionType = "assign";
    }


    var replayData = { "type": "replayTask", "replayData": "{'subject': '" + subject + "','actionType': '" + actionType + "','taskID': '" + taskID + "','projectId': '" + projectId + "', 'content': '" + content + "','importance': '" + importance + "','dataChange_LastTime': '" + dataChange_LastTime + "','state': '" + state + "','handleMan': '" + handleMan + "'}" }

    $.ajax({
        type: "GET",
        url: "AjaxHandler.ashx",
        cache: false,
        data: replayData,
        success: function (result) {
            if (result == "true") {
                alert('添加成功');
            }
            else {
                alert('添加失败');
            }
        }
    });
});

//分派任务按钮
$("#cbIsAssign").live("click", function () {
    var obj = $(this);
    if (obj[0].checked) {
        $("#ddlHandleMan").removeAttr("disabled");
    }
    else {
        $("#ddlHandleMan").attr("disabled", "disabled");
    }
});

$("#lnkLogout").live("click", function () {
    debugger;
    $.ajax({
        type: "GET",
        url: "AjaxHandler.ashx",
        cache: false,
        data: { "type": "delCookies" },
        success: function (result) {
            location.href = "login.aspx";
        }
    });
});



//模块切换
$("#divDefault,#divDiagram,#divAdmin").live("click", function () {
    var objID = $(this).attr("id");
    switch (objID) {
        case "divDefault":
            $("#divHticket").css("display", "");
            $("#divDiagramContent,#divAdminContent").css("display", "none");
            break;
        case "divDiagram":
            $("#divDiagramContent").css("display", "");
            $("#divHticket,#divAdminContent").css("display", "none");
            break;
        case "divAdmin":
            $("#divAdminContent").css("display", "");
            $("#divHticket,#divDiagramContent").css("display", "none");
            break;
    }
});

$("#lnkDefault,#lnkDiagram,#lnkAdmin").live("click", function () {
    $("#lnkDefault,#lnkDiagram,#lnkAdmin").removeClass("selected");
    var $obj = $(this);
    var id = $obj.attr("id");
    $obj.addClass("selected");

    if (id == "lnkDefault") {
        $("#divShowAddTask").css("display", "none");
        $("#divShowTask").css("display", "");
    }
});


$("#divMyTask,#divNewTask,#divProcessTask,#divAllTask").live("click", function () {
    var $obj = $(this);
    var id = $obj.attr("id");
});

//图形统计
$("#divbtnDiagram").live("click", function () {
    var startTime = $("#txtStartDate").val();
    var endTime = $("#txtEndDate").val();

    $.ajax({
        type: "GET",
        cache: "false",
        url: "AjaxHandler.ashx",
        data: { "type": "chart", "startTime": startTime, "endTime": endTime },
        success: function (result) {
            var xml = result;
            $("#divData").data("chart", xml);
            var type = $(":radio[checked='checked']").attr("id");
            renderChart(type);
        }
    });
});

$(":radio").live("click", function () {
    var $obj = $(this);
    var type = $obj.attr("id");
    renderChart(type);
});

function renderChart(type) {
    var myChart = new FusionCharts("/FusionCharts/" + type + ".swf", "myChartId", "500", "400");
    var xml = $("#divData").data("chart");
    if (xml == undefined) {
        return;
    }
    myChart.setDataXML(xml);
    myChart.render("divChart");
}


//Test
$("#Test").live("click", function () {
    $.ajax({
        type: "GET",
        url: "History.aspx",
        cache: false,
        success: function (result) {
            var obj = $(result).find("#divHistory");
            $("#divShowHistory").html(obj[0].outerHTML);
            InitEdit("txtHistoryContent");
            $("#divShowHistory").css("display", "");
            $("#divShowTask,#divShowAddTask").css("display", "none");
        }
    });
});
